package model

import (
	"dsc-server/domain/req"
	"strconv"
	"time"
)

type BaseModel struct {
	ID            uint   `gorm:"type:int(11);primaryKey;column:id;comment:id;AUTO_INCREMENT" `
	CreateOrgCode string `gorm:"type:varchar(11);column:create_org_code;comment:创建组织;default:'';not null" doc:"创建组织"`
}
type CommonModel struct {
	IsDeleted  int       `gorm:"type:tinyint(2);column:is_deleted;comment:是否删除【0：未删除 1：删除】;default:0;not null" doc:"是否删除【0：未删除 1：删除】"`
	CreateBy   int       `gorm:"type:int(11);column:create_by;comment:创建人;default:0;not null" doc:"创建人"`
	CreateTime time.Time `gorm:"type:datetime;column:create_time;comment:创建时间;autoCreateTime;not null;default:CURRENT_TIMESTAMP" doc:"创建时间"`
	UpdateBy   int       `gorm:"type:int(11);column:update_by;comment:更新人;default:0;not null" doc:"更新人"`
	UpdateTime time.Time `gorm:"type:datetime;column:update_time;comment:更新时间;autoUpdateTime;not null;default:CURRENT_TIMESTAMP" doc:"更新时间"`
	CreateName string    `gorm:"type:varchar(50);column:create_name;comment:创建人名称;default:'';not null" doc:"创建人名称"`
	UpdateName string    `gorm:"type:varchar(50);column:update_name;comment:更新人名称;default:'';not null" doc:"更新人名称"`
}

type CacheSetting struct {
	BaseModel
	Url        string `gorm:"column:url;comment:前台访问的url" doc:"url"`
	Expire     int    `gorm:"column:expire;comment:缓存过期时间" doc:"缓存过期时间"`
	Rule       string `gorm:"column:rule;comment:缓存规则" doc:"缓存规则"`
	ServerName string `gorm:"server_name;comment:服务名" doc:"服务名"`
	KeyName    string `gorm:"key_name;comment:缓存md5Key" doc:"缓存md5Key"`
	CommonModel
}

// TableName  cache_setting
func (c *CacheSetting) TableName() string {
	return "cache_setting"
}

type CacheViewHistory struct {
	BaseModel
	Url        string `gorm:"column:url;comment:前台访问的url" doc:"url"`
	ServerName string `gorm:"column:server_name;comment:服务名" doc:"服务名"`
	KeyName    string `gorm:"column:key_name;comment:缓存md5Key" doc:"缓存md5Key"`
	Action     string `gorm:"column:action;comment:缓存动作,set,get" doc:"缓存动作,set,get"`
	CommonModel
}

// ApiConfig API配置表，存储API接口的相关配置信息
type ApiConfig struct {
	BaseModel
	Path         string `gorm:"column:path;type:varchar(255);uniqueIndex;comment:'API路径，唯一'" json:"path"`
	Name         string `gorm:"column:name;type:varchar(255);comment:'API名称'" json:"name"`
	Note         string `gorm:"column:note;type:varchar(255);comment:'API备注说明'" json:"note"`
	Sql          string `gorm:"column:sql;type:text;comment:'执行的SQL语句'" json:"sql"`
	Params       string `gorm:"column:params;type:text;comment:'API请求参数'" json:"params"`
	Status       int    `gorm:"column:status;type:int(11);comment:'API状态(0-禁用,1-启用等)'" json:"status"`
	DatasourceID int    `gorm:"column:datasource_id;type:int(11);comment:'关联的数据源ID'" json:"datasource_id"`
	RealSql      string `gorm:"column:real_sql;type:text;comment:'实际执行的SQL(可能经过参数替换)'" json:"real_sql"`
	IsSelect     int    `gorm:"column:is_select;type:int(11);comment:'是否为查询操作(1-查询,0-非查询)'" json:"is_select"`
	Previlege    int    `gorm:"column:previlege;type:int(11);comment:'权限级别'" json:"previlege"`
	GroupID      int    `gorm:"column:group_id;type:int(11);comment:'所属API分组ID'" json:"group_id"`
	CommonModel
}

// TableName 指定表名
func (a *ApiConfig) TableName() string {
	return "api_config"
}

// TableName  cache_view_history
func (c *CacheViewHistory) TableName() string {
	return "cache_view_history"
}

// Datasource 数据源配置表，存储数据库连接信息
type Datasource struct {
	BaseModel
	Name     string `gorm:"column:name;type:varchar(255);comment:'数据源名称'" json:"name"`
	Note     string `gorm:"column:note;type:varchar(255);comment:'数据源备注'" json:"note"`
	Type     string `gorm:"column:type;type:varchar(255);comment:'数据源类型(mysql,postgres等)'" json:"type"`
	Url      string `gorm:"column:url;type:varchar(255);comment:'数据库连接URL'" json:"url"`
	Username string `gorm:"column:username;type:varchar(255);comment:'数据库用户名'" json:"username"`
	Password string `gorm:"column:password;type:varchar(255);comment:'数据库密码'" json:"password"`
	CommonModel
}

// TableName 指定表名
func (d *Datasource) TableName() string {
	return "datasource"
}

// ApiAuth API权限关联表，关联Token与API分组的权限关系
type ApiAuth struct {
	BaseModel
	TokenID int `gorm:"column:token_id;type:int(11);comment:'关联的TokenID'" json:"token_id"`
	GroupID int `gorm:"column:group_id;type:int(11);comment:'关联的API分组ID'" json:"group_id"`
	CommonModel
}

// TableName 指定表名
func (a *ApiAuth) TableName() string {
	return "api_auth"
}

// ApiGroup API分组表，用于对API进行分组管理
type ApiGroup struct {
	BaseModel
	Name string `gorm:"column:name;type:varchar(255);uniqueIndex;comment:'分组名称，唯一'" json:"name"`
	CommonModel
}

// TableName 指定表名
func (a *ApiGroup) TableName() string {
	return "api_group"
}

// Token 访问令牌表，存储API访问的令牌信息
type Token struct {
	BaseModel
	Token  string `gorm:"column:token;type:varchar(255);comment:'访问令牌字符串'" json:"token"`
	Note   string `gorm:"column:note;type:varchar(255);comment:'令牌备注说明'" json:"note"`
	Expire int64  `gorm:"column:expire;type:bigint(20);comment:'过期时间戳(毫秒)'" json:"expire"`
	CommonModel
}

// TableName 指定表名
func (t *Token) TableName() string {
	return "token"
}

// User 系统用户表，存储系统登录用户信息
type User struct {
	BaseModel
	Username string `gorm:"column:username;type:varchar(255);uniqueIndex;comment:'用户名，唯一'" json:"username"`
	Password string `gorm:"column:password;type:varchar(255);comment:'用户密码(通常加密存储)'" json:"password"`
	CommonModel
}

// TableName 指定表名
func (u *User) TableName() string {
	return "user"
}
func InstanceGetCacheViewHistory(req *req.CacheReq) *CacheViewHistory {
	instance := &CacheViewHistory{}
	instance.ServerName = req.ServerName
	instance.KeyName = req.KeyName
	instance.CreateBy = req.CreateBy
	instance.CreateName = req.CreateName
	instance.Url = req.Url
	instance.UpdateBy = req.UpdateBy
	instance.UpdateName = req.UpdateName
	instance.CreateOrgCode = strconv.Itoa(req.CreateOrgCode)
	instance.CreateTime = time.Now()
	instance.UpdateTime = time.Now()
	instance.Action = "get"
	return instance
}

func InstanceSetCacheViewHistory(req *req.CacheSetReq) *CacheViewHistory {
	instance := &CacheViewHistory{}
	instance.ServerName = req.ServerName
	instance.KeyName = req.KeyName
	instance.CreateBy = req.CreateBy
	instance.CreateName = req.CreateName
	instance.Url = req.Url
	instance.UpdateBy = req.UpdateBy
	instance.UpdateName = req.UpdateName
	instance.CreateOrgCode = strconv.Itoa(req.CreateOrgCode)
	instance.CreateTime = time.Now()
	instance.UpdateTime = time.Now()
	instance.Action = "set"
	return instance
}
